﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Storage;

namespace Dynamics.NAV.Metro.Common
{
    public class SettingsStorage
    {
        public static T GetLocalSetting<T>(string key)
        {
            T result = default(T);

            ApplicationDataContainer con = GetContainer();

            if (con.Values.ContainsKey(key))
            {
                result = (T)con.Values[key];
            }

            return result;
        }

        public static bool LocalSettingsExist(string key)
        {
            return GetContainer().Values.ContainsKey(key);
        }

        public static void SetLocalSetting(string key, Object value)
        {
            GetContainer().Values[key] = value;
        }

        private static ApplicationDataContainer GetContainer()
        {
            ApplicationDataContainer con;

            if (ApplicationData.Current.LocalSettings.Containers.ContainsKey(App.SETTINGS_CONTAINER))
                con = ApplicationData.Current.LocalSettings.Containers[App.SETTINGS_CONTAINER];
            else
                con = ApplicationData.Current.LocalSettings.CreateContainer(App.SETTINGS_CONTAINER, ApplicationDataCreateDisposition.Always);
            
            return con;
        }
    }
}
